package com.chookss.tiku

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.Urls
import com.chookss.tiku.entity.LiveResult
import com.chookss.tiku.entity.Subject
import com.chookss.tiku.entity.SubjectDetailEntity
import com.chookss.tiku.response.PracticeSubjectList
import com.chookss.tiku.response.SubjectDetailResponse
import com.chookss.tools.Utils
import com.chookss.tools.httpForm
import com.chookss.tools.old.SignUtils
import com.johnrambo.ktea.common.logger
import com.johnrambo.ktea.common.parseObject
import com.johnrambo.ktea.net.http.Method
import com.johnrambo.ktea.net.http.http

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/01/06
 * Note: 试卷记录题目列表
 */
class ExamRecordSubjectViewModel() : BasePracticeTestViewModel() {

    private val pageSize = 100
    lateinit var practiceType: String
    var examCode:String? = null
    var periodName:String? = null
    var recordUid:String? = null
    var isMiss:String? = null
    lateinit var memoryLvl: String
    lateinit var subjectTypeCode: String
    //题目列表
    val subjectPageMap = mutableMapOf<Int, List<Subject>>()
    //题目列表
    val subjectLists = mutableListOf<Subject>()
    private fun createSubjectResult(subjectList: List<Subject>, listIndex: Int): LiveResult<Subject> {
        if (subjectList.size > listIndex) {
            return LiveResult(subjectList[listIndex])
        } else {//没有结果,结束了
            return LiveResult(null)
        }
    }

    //下载题目列表
    override fun getSubject(index: Int): LiveData<LiveResult<Subject>> {
        var liveResult = MutableLiveData<LiveResult<Subject>>()

        var pageNum = index / pageSize
        var listIndex = index % pageSize
        if(index!=0){
            var subjectList = subjectPageMap[pageNum]
            if (subjectList != null) {
                liveResult.postValue(createSubjectResult(subjectList, listIndex))
                return liveResult
            }
        }
        var url2 = ""
        val params2: MutableMap<String, Any?> = HashMap()
        if ("3" == practiceType||"4" == practiceType) {
            params2["examCode"] = examCode
            params2["recordUid"] = recordUid
            params2["isMiss"] = isMiss
            url2 = "api/exam/getExamSubject"
        } else {
            params2["practiceType"] = practiceType
            params2["recordUid"] = recordUid
            url2 = "api/practice/getPracticeSubject"
        }
        if (!Utils.isNull(memoryLvl)) {
            params2["memoryLvl"] = memoryLvl
        }
        if (!Utils.isNull(subjectTypeCode)) {
            params2["subjectTypeCode"] = subjectTypeCode
        }
        params2["periodName"] = periodName
        params2["pageNum"] = (pageNum+1).toString()
        params2["pageSize"] = pageSize.toString()
        val timestamp = SignUtils.getTimeStamp() //20201208125238
        val nonce = SignUtils.getRandomString(14)
        val sign = SignUtils.makeSignStr(params2, timestamp, nonce, false)
        logger("sign:$sign")
        params2["timestamp"] = timestamp
        params2["nonce"] = nonce
        params2["sign"] = sign

        //api/practice/getPracticeSubject   practiceType
        //api/exam/getExamSubject  examCode

        jobs + http {
            url = url2
            method = Method.POST
            params = params2
            onSuccess = {
                var result = it.parseObject<PracticeSubjectList.Response>()
                if (result?.code != 200) {
                    liveResult.postValue(LiveResult(Error(result?.msg)))
                } else {
                    if(pageNum==0){
                        subjectLists.clear()
                        subjectPageMap.clear()
                    }
                    var subjectList = result.data ?: emptyList()
                    subjectLists.addAll(subjectList)
                    subjectPageMap[pageNum] = subjectList
                    liveResult.postValue(createSubjectResult(subjectList, listIndex))
                }
            }
            onError = {
                liveResult.postValue(LiveResult(Error("服务器异常，请稍后重试")))
            }
        }
        return liveResult
    }
    //获取题目详情

     fun getRecordUidSubjectDetail(answerId:String,subject: Subject):LiveData<LiveResult<SubjectDetailEntity>> {
        var liveResult = MutableLiveData<LiveResult<SubjectDetailEntity>>()

        val params2: MutableMap<String, Any?> = HashMap()
        params2["subjectCode"] = subject.subjectCode
        params2["recordUid"] = recordUid
        params2["recordType"] = practiceType
        params2["answerId"] = answerId
        jobs + httpForm<SubjectDetailResponse> {
            url = Urls.getSituationRecord
            params = params2
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }



    //提交答案
    override fun submitAnswer(subject: Subject, answers:List<String>, time:Long) =  MutableLiveData<LiveResult<Any>>()

}
